/**
 * 
 */
package com.alstudio.module.push;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.alstudio.module.BaseModule;
import com.alstudio.module.BaseModuleEvent;
import com.alstudio.module.ModuleEvent;
import com.alstudio.module.ModuleType;
import com.alstudio.utils.android.MetaValueUtils;
import com.alstudio.utils.log.ALLog;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;

/**
 * 百度推送服务管理器
 * 
 * @author alonso lee
 * 
 */
public class ALPushManager extends BaseModule {

	private static ALPushManager defaultPushManager = null;

	public static ALPushManager getDefaultPushManager() {
		if (defaultPushManager == null) {
			defaultPushManager = new ALPushManager();
		}
		return defaultPushManager;
	}

	// 用share preference来实现是否绑定的开关。在ionBind且成功时设置true，unBind且成功时设置false
	public boolean isBind(Context context) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(context);
		String flag = sp.getString("push_bind_flag", "");
		if ("ok".equalsIgnoreCase(flag)) {
			return true;
		}
		return false;
	}

	public void setBind(Context context, boolean flag) {
		String flagStr = "not";
		if (flag) {
			flagStr = "ok";
		}
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = sp.edit();
		editor.putString("push_bind_flag", flagStr);
		editor.commit();
	}

	/**
	 * 启动百度推送服务。应用程序在启动的activity中调用此方法。 此方法会判断应用是否已经绑定到百度推送服务中。如果已经绑定，那么不会执行
	 * 绑定动作
	 * 
	 * @param context
	 */
	public void startPushServer(Context context) {
		if (isBind(context)) {
			ALLog.d("推送已经绑定，不需要重新绑定");
		} else {
			String apiKey = MetaValueUtils.getMetaValue(context, "api_key");
			PushManager.startWork(context, PushConstants.LOGIN_TYPE_API_KEY,
					apiKey);

			ALLog.d("开始绑定推送服务");
			ArrayList<String> tags = new ArrayList<String>();
			tags.add("alonso");
			PushManager.setTags(context, tags);
		}
	}

	public void setNotifycationStyle() {
		// PushManager.setNotificationBuilder(context, id, notificationBuilder);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.module.BaseModule#registerModule(java.lang.Object)
	 */
	@Override
	public void registerModule(Context context) {
		// TODO Auto-generated method stub
		startPushServer(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.module.BaseModule#unregisterModule(java.lang.Object)
	 */
	@Override
	public void unregisterModule(Context context) {
		// TODO Auto-generated method stub
		PushManager.stopWork(context);
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.BaseModule#onModuleManagerEvent(com.alstudio.module
	 * .BaseModule)
	 */
	@Override
	public void onModuleManagerEvent(Object event) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.module.BaseModule#newModuleEvent()
	 */
	@Override
	public ModuleEvent newModuleEvent() {
		return null;

	}

	/* (non-Javadoc)
	 * @see com.alstudio.module.BaseModule#setHandleEvent(java.lang.Class)
	 */
	@Override
	public void setHandleEvent(Class<?> event) {
		// TODO Auto-generated method stub
		
	}

}
